<?php

class User_addressController extends BaseController {
    
    //地址信息
    public function infoAction() {
        $addressId = Request::getParams('id',0);
//        if ($addressId && !$addressInfo = self::model('user_address')->getOne('id=?', $addressId)) {
//            Response::ajax(NORMAL_ERROR, '信息查询失败，请稍后重试');
//        }
//        if($addressInfo['uid']!=$this->user_info['uid']) {
//            Response::ajax(NORMAL_ERROR, '地址信息获取失败');
//        }
        if($addressId!=0){
            $addressInfo = self::model('user_address')->getOne('id=?', $addressId);
            $address=self::model('user_address')->formateData($addressInfo);
        }else{
            $address=array();
        }
        Response::ajax(200, 'success', $address);
    }
    
    //编辑、添加地址
    public function editAction() {
        $addressId = Request::getParams('id');
        $data = [];
        $region=['上海市','北京市','天津市','重庆市'];
        $data['uid'] = $this->user_info['uid'];
        $data['mobile'] = Request::getParams('mobile');
        $data['name'] = Request::getParams('name');
        $data['province'] = Request::getParams('province');
        $data['city'] = Request::getParams('city');
        $data['area'] = Request::getParams('area');

        if(in_array($data['province'],$region)){
            $province=mb_substr($data['province'], 0, 2);
        }else{
            $province=$data['province'];
        }
        $data['province_code']=$this->regionCode($province);
        $data['city_code'] = $this->regionCode($data['city'],$data['province_code']);
        $data['area_code'] = $this->regionCode($data['area'],$data['city_code']);
        $data['address'] = Request::getParams('address');
        $data['is_default'] = intval(Request::getParams('isDefault'));
        $addressInfo = [];
        if ($addressId && !$addressInfo = self::model('user_address')->getOne('id=?', $addressId)) {
            Response::ajax(NORMAL_ERROR, '信息查询失败，请稍后重试');
        }
        $result = user_addressModel::validata($data);
        if($result!==true) {
            Response::ajax(NORMAL_ERROR, $result);
        }
        if($data['is_default']) {
            self::model('user_address')->update([
                'is_default'=> 0
            ], 'uid=?', [$this->user_info['uid']]);
        }
        if ($addressId) {//修改
            if($addressInfo['uid']!=$this->user_info['uid']) {
                Response::ajax(NORMAL_ERROR, '修改地址信息失败');
            }
            if (self::model('user_address')->update($data, 'id=?', $addressId)) {
                $code=$this->dealerAddress($addressInfo['area_code'],$data['area_code']);
                Response::ajax($code, '修改成功');
            }
        } else {//新增
            if(!self::model('user_address')->count(1, 'uid=?', $this->user_info['uid'])) {
                $data['is_default'] = 1;
            }
            self::model('user_address')->insert($data);
            Response::ajax(200, '添加成功');

        }
        Response::ajax(SYSTEM_ERROR, '数据处理失败，请稍后重试');
    }



    //地址列表
    public function listAction() {
        Response::ajax(200, 'success', self::model('user_address')->formateData(
                self::model('user_address')
                ->order('is_default desc')
                ->getAll('uid=?', $this->user_info['uid'])
                ));
    }
    
    //删除地址
    public function deleteAction() {
        $addressIds = Request::getParams('id');
        if (!$addressIds) {
            Response::ajax(NORMAL_ERROR, '参数传递有误,删除失败');
        }
        $addressList = self::model('user_address')->getByIds($addressIds);
        if($addressList) {
            foreach ($addressList as $addressInfo) {
                if($addressInfo['uid']!=$this->user_info['uid']) {
                    Response::ajax(NORMAL_ERROR, '删除失败');
                }
            }
            if(self::model('user_address')->deleteByIds($addressIds)) {
                Response::ajax(200, '删除成功');
            }
            Response::ajax(SYSTEM_ERROR, '删除失败,请稍后重试');
        }else{
            Response::ajax(200, '删除成功');
        }
    }
    
    public function getDefaultAction() {
        $addressInfo = self::model('user_address')->order('is_default desc')->getOne('uid=?', $this->user_info['uid']);
        Response::ajax(200, 'success', self::model('user_address')->formateData($addressInfo));
    }
    
    //设为默认
    public function defaultAction() {
        $addressId = Request::getParams('id');
        if ($addressId && !$addressInfo = self::model('user_address')->getOne('id=?', $addressId)) {
            Response::ajax(NORMAL_ERROR, '地址信息获取失败,设置失败');
        }
        if($addressInfo['uid']!=$this->user_info['uid']) {
            Response::ajax(NORMAL_ERROR, '地址信息获取失败,设置失败');
        }
        if(self::model('user_address')->update(['is_default'=>0], 'uid=?', $this->user_info['uid'])) {
            self::model('user_address')->update(['is_default'=>1], 'id=?', $addressId);
            Response::ajax(200, '设置默认地址成功');
        }
        Response::ajax(SYSTEM_ERROR, '设置默认地址失败,请稍后重试');
    }

    private function regionCode($region_name,$region=null){
        if(empty($region)){
            $info=self::model("region")->getOne(['name=?','parent_id=?'],[$region_name,0]);
        }else{
            $info=self::model("region")->getOne(['name=?','parent_id=?'],[$region_name,$region]);
        }

        return $info['id'];
    }
    private function regionName($region){
        $info=self::model("region")->find($region);
        return $info['name'];
    }

    private function dealerAddress($o_area,$n_area){
        $uid=$this->user_info['uid'];
        $o_info=self::model("dealer_address")->getOne('area=?',$o_area);
        $n_info=self::model("dealer_address")->getOne('area=?',$n_area);
        if($o_info['dealer_id']!=$n_info['dealer_id']){
            self::model("user_cart")->delete('uid=?',$uid);
            return 201;
        }else{
            return 200;
        }
    }
    
}